package com.paas.form.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.paas.common.model.BaseUserInfo;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.UniqueIdUtil;
import com.paas.common.web.annotion.OperLog;
import com.paas.common.web.controller.BaseController;
import com.paas.common.web.oper.OprLogConstant;
import com.paas.form.entity.FormGroupEntity;
import com.paas.form.service.IFormGroupService;
import com.paas.form.vo.FormGroupVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/form/formGroup")
@Api(description = "mdm", tags = "")
public class FormGroupController extends BaseController {

    @Autowired
    public IFormGroupService formGroupService;

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @OperLog(operType = OprLogConstant.ADD, operModul = "", operDescribe = "-保存")
    public void save(@RequestBody FormGroupEntity formGroupEntity) throws ParseException {
        if (BeanUtil.isNotEmpty(formGroupEntity)) {
            injectUpdateDataAuth(formGroupEntity);
        }
        formGroupService.save(formGroupEntity);
    }

    @RequestMapping(value = "/addGroup", method = RequestMethod.POST)
    @ApiOperation(value = "保存信息", notes = "保存信息")
    @OperLog(operType = OprLogConstant.ADD, operModul = "", operDescribe = "-保存")
    public void addGroup(@RequestBody FormGroupEntity formGroupEntity) throws ParseException {
        if (BeanUtil.isNotEmpty(formGroupEntity)) {
            injectUpdateDataAuth(formGroupEntity);
        }
        BaseUserInfo userInfo = getUserInfo();
        formGroupEntity.setId(UniqueIdUtil.getIntGuid());
        formGroupEntity.setOrgId(userInfo.getOrgId());
        formGroupEntity.setAppId(userInfo.getAppId());
        formGroupEntity.setCreateId(userInfo.getUserId());
        formGroupEntity.setCreateDate(new Date());
        formGroupEntity.setModifyId(userInfo.getUserId());
        formGroupEntity.setModifyDate(new Date());
        formGroupService.insert(formGroupEntity);
    }

    @RequestMapping(value = "/saveBatch", method = RequestMethod.POST)
    @ApiOperation(value = "批量保存信息", notes = "批量保存信息")
    @OperLog(operType = OprLogConstant.UPDATE, operModul = "", operDescribe = "-批量保存修改")
    public void saveBatch(@RequestBody List<FormGroupEntity> list) throws ParseException {
        if (list != null && list.size() > 0) {
            injectUpdateDataAuth(list);
        }
        formGroupService.saveBatch(list);
    }

    @RequestMapping(value = "/deleteById", method = RequestMethod.GET)
    @ApiOperation(value = "根据Id删除数据", notes = "根据Id删除数据")
    @OperLog(operType = OprLogConstant.DELETE, operModul = "", operDescribe = "-删除")
    public void deleteById(String id) {
        if (null != id && !"".equals(id)) {
            FormGroupEntity formGroupEntity = new FormGroupEntity();
            injectUpdateDataAuth(formGroupEntity);
            formGroupEntity.setId(id);
            formGroupService.deleteByEntity(formGroupEntity);
        }
    }

    @RequestMapping(value = "/deleteBatchByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据Id批量删除数据", notes = "根据Id批量删除数据")
    @OperLog(operType = OprLogConstant.DELETE, operModul = "", operDescribe = "-批量删除")
    public void deleteBatchByIds(@RequestBody List<String> ids) {
        if (null != ids && ids.size() > 0) {
            FormGroupEntity formGroupEntity = new FormGroupEntity();
            injectUpdateDataAuth(formGroupEntity);
            //formGroupService.deleteBatchIds(ids);
            formGroupService.deleteBatchIdsWithPermission(ids, formGroupEntity);
        }
    }

    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    @ApiOperation(value = "根据Id查询对象", notes = "根据Id查询对象")
    public FormGroupEntity queryById(String id) {
        FormGroupEntity bean = formGroupService.getById(id);
        return bean;
    }

    @RequestMapping(value = "/queryBatchByIds", method = RequestMethod.POST)
    @ApiOperation(value = "根据Id查询对象列表", notes = "根据Id查询对象列表")
    public List queryBatchByIds(@RequestBody List<String> ids) {
        List list = formGroupService.getBatchByIds(ids);
        return list;
    }

    @RequestMapping(value = "/queryPage", method = RequestMethod.GET)
    @ApiOperation(value = "模板分页查询", notes = "模板分页查询")
    public IPage queryPage(FormGroupEntity formGroupEntity, Integer currentPage, Integer pageSize) {
        if (currentPage == null) {
            currentPage = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        injectCheckDataAuth(formGroupEntity);
        IPage pageBean = formGroupService.getPage(formGroupEntity, currentPage, pageSize);
        return pageBean;
    }

    @RequestMapping(value = "/queryList", method = RequestMethod.GET)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    public List queryList(FormGroupEntity formGroupEntity) {
        injectCheckDataAuth(formGroupEntity);
        List list = formGroupService.getList(formGroupEntity);
        return list;
    }

    @RequestMapping(value = "/queryGroupTree", method = RequestMethod.GET)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    public List<FormGroupVo> queryGroupTree() {
        return formGroupService.queryGroupTree();
    }

    @RequestMapping(value = "/queryGroupAndViewTree", method = RequestMethod.GET)
    @ApiOperation(value = "模板查询", notes = "模板查询")
    public List<FormGroupVo> queryGroupAndViewTree() {
        return formGroupService.queryGroupAndViewTree();
    }
}